import { createApp } from 'vue'
import { createPinia } from 'pinia'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import 'normalize.css'
import './style.css'
import './assets/styles/common.scss'
import './theme/dark.scss'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

import App from './App.vue'
import router from './router'
import { permission, superAdmin } from './directives/permission'
import { startTokenExpirationCheck, setGlobalRouter } from './utils/token'
import { applyTheme } from './theme'
import { useAppStore } from './stores/app'

const app = createApp(App)
const pinia = createPinia()

// 注册Element Plus图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}

app.use(pinia)
app.use(router)
app.use(ElementPlus)

// 设置全局router实例
setGlobalRouter(router)

// 注册权限指令
app.directive('permission', permission)
app.directive('super-admin', superAdmin)

app.use(pinia)
app.use(router)

// 启动token过期检查
startTokenExpirationCheck()

// 初始化主题系统
const appStore = useAppStore()
appStore.initializeSettings()
applyTheme(appStore.theme, appStore.primaryColor)

app.mount('#app')